package com.xbdun.web.validatebmp.facade.upload;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.upload.request.UploadByBASE64Request;
import com.xbdun.facade.validatebmp.constant.WatermarkPostion;
import com.xbdun.facade.validatebmp.exceptions.ValidateBizException;
import com.xbdun.facade.validatebmp.menus.UploadFileFormat;
import com.xbdun.facade.validatebmp.request.OpenUploadReqeust;
import com.xbdun.facade.validatebmp.service.upload.OpenUploadFacade;
import com.xbdun.facade.validatebmp.upload.core.AllowUploadType;
import com.xbdun.facade.validatebmp.upload.core.UploadFileBASE64;
import com.xbdun.facade.validatebmp.upload.result.UploadOpenResult;
import com.xbdun.web.validatebmp.upload.core.UploadTypeUtil;
import com.xbdun.web.validatebmp.upload.core.service.UploadManageOpenService;
import com.xbdun.web.validatebmp.upload.service.receive.UploadOpenServiceImpl;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class OpenUploadFacadeImpl implements OpenUploadFacade{

	@Autowired
	UploadManageOpenService uploadManageOpenService;
	@Resource
	UploadOpenServiceImpl uploadOpenServiceImpl;
	public XbdUnStringResult getUrl(OpenUploadReqeust request) throws ValidateBizException {
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			if(request.getFiledId().length==0){
				result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("获取图片地址失败，fileId不能为空"));
			}
			UploadFileFormat valueOfKey = UploadFileFormat.valueOfKey(request.getUploadFormat());
			if(null==valueOfKey){
				result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("获取图片地址失败，UploadFileForma系统未定义"));
			}
			if(result.getSuccess()){
				//返回真实的图片地址
				result.setData(uploadOpenServiceImpl.getUrl(request.getFiledId()[0],valueOfKey));
			}
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取图片地址异常,",e);
		}
		return result;
	}

	public Map<Long, String> getUrlsMap(OpenUploadReqeust request) {
		try{
			UploadFileFormat valueOfKey = UploadFileFormat.valueOfKey(request.getUploadFormat());
			if(null==valueOfKey){
				return null;
			}
			return uploadOpenServiceImpl.getUrlsMap(valueOfKey, request.getFiledId());
		}catch(Exception e){
			log.error("获取图片地址-getUrlsMap-异常,",e);
		}
		return null;
	}
	public int deletes(OpenUploadReqeust request){
		try{
			if(request.getFiledId().length==0){
				return 0;
			}
			return uploadManageOpenService.delete(request.getFiledId());
		}catch(Exception e){
			log.error("删除图片,入参,{}异常,",request,e);
		}
		return 0;
	}
	
	public UploadOpenResult uploadByBASE64(UploadByBASE64Request request){
		UploadOpenResult result = null;
		try{
			AllowUploadType allowUploadType = chekUploadByBASE64(request);
			
			//上传文件
			UploadFileBASE64 upFileBASE64 = new UploadFileBASE64();
			
			upFileBASE64.setExtName(request.getFileBase64().getExtName());
			upFileBASE64.setFile(request.getFileBase64().getFile());
			upFileBASE64.setFileSize(allowUploadType.getSize());
			upFileBASE64.setUploadBusinessType(allowUploadType.getUploadBusinessType());
			upFileBASE64.setReturnFormat(request.getReturnFormat());//设置上传返回类型
			//文件上传
			result = uploadOpenServiceImpl.uploadByBASE64(WatermarkPostion.valueOf(request.getWatermarkPostion()), request.getOrgCode(),upFileBASE64);
		}catch (Exception e) {
			result = new UploadOpenResult();
			result.setBizException(e);
		}finally{
			//文件Base64
			if(null!=request.getFileBase64()){
				request.getFileBase64().setExtName(null);
				request.getFileBase64().setFile(null);
				request.setFileBase64(null);
			}
		}
		return result;
		
	}
	
	//检测上传文件
	private AllowUploadType chekUploadByBASE64(UploadByBASE64Request request) {
		StringBuilder sb = new StringBuilder();
		//获取上传文件类型
		AllowUploadType allowUploadType = UploadTypeUtil.getAllowUploadType(request.getUploadType());
		if(null==allowUploadType){
			sb.append("["+request.getUploadType()+"]上传类型系统未定义;");
		}
		if(StringUtils.isBlank(request.getOrgCode())){
			sb.append("上传机构编号未定义");
		}
		if(null==request.getFileBase64()){
			sb.append("上传文件不能为空");
		}
		//异常
		if(sb.length()>0){throw BizException.PARAM_CHKE_FAIL.newInstance(sb.toString());}
		sb.setLength(0);
		sb = null;
		return allowUploadType;
	}
}
